<?php
/**
 * class模板
 *
 * PHP version 5.6
 *
 * @category  API
 * @package   YII
 * @author    saruri <saruri@163.com>
 * @copyright 2006-2021 saruri
 * @license   https://saruri.cn/licence.txt BSD Licence
 * @link      http://saruri.cn
 * @date      2021/03/11 14:04:08
 */
//namespace console\models;
//use Yii;
class Terms
{
    private $_config = [];
    
    //初始化
    public function __construct()
    {
        //各种配置
          
        //$this->mysqli =new mysqli("123.58.217.137", "114chuhai_com", "DcRsYHY5mEeLnfZK", '114chuhai_com'); //实例化一个mysqli对象,并打开一个连接
        $this->mysqli =new mysqli("123.58.217.137", "new_114chuhai_co", "EC4m5pEEW7aDLkMZ", 'new_114chuhai_co'); //实例化一个mysqli对象,并打开一个连接
        if (mysqli_connect_errno()) { //检查是否可以正确打开数据库
            echo "<font color='red'>unable to connect!</font>";
        }
    }
      
    //继承上级的初始化
    public function init()
    {
        //可选
    }
      
    public function run()
    {
        //主流程
        //var_dump($this->countryNameArr);
        //var_dump($this->taxonomyArr);
        return $this->countryNameArr;
        self::close();
        exit("执行完成");
    }

    /*
    *  @desc      关闭
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 14:05:39
    */
    public function close()
    {
        //do somehing
        $this->mysqli->close(); //关闭数据库连接
        exit("关闭连接");
    }

    /*
    *  @desc      获取国名数组
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 14:06:45
    */
    public function getCountryNameArr()
    {
        //do somehing
        $where = " WHERE country_code <>'' ";
        $SQL_SELECT_CODE="SELECT * FROM  wp_terms".$where;
        $CODE=$this->mysqli->query($SQL_SELECT_CODE);
        while ($arr1=$CODE->fetch_array()) {
            $this->countryNameArr[$arr1['name']]=$arr1;
        }
        return $this;
    }

    /*
    *  @desc      获取国名分类数组 taxonomy
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 14:06:45
    */
    public function getTaxonomyArr()
    {
        //do somehing
        $where = " WHERE country_code <>'' ";
        $SQL_SELECT_CODE="SELECT * FROM  wp_term_taxonomy";//.$where;
        $CODE=$this->mysqli->query($SQL_SELECT_CODE);
        while ($arr1=$CODE->fetch_array()) {
            $this->taxonomyArr[$arr1['term_id']]=$arr1;
        }
        return $this;
    }


    /* 
    *  @desc      获取结果数组
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 14:32:49  
    */ 
    public function  getResult()
    {
        //do somehing

        foreach ($this->countryNameArr as $key => $value) {
            # code...
            $arr=$this->taxonomyArr[$value['term_id']];
            $this->countryNameArr[$key]['taxonomy_id']=$arr['term_taxonomy_id'];

            $this->countryNameArr[$key]['parent']=$arr['parent'];
            $this->countryNameArr[$key]['taxonomy']=$arr['taxonomy'];
        }

        return $this;
         
    }


    /*
    *  @desc      获取国家相关的关系 废弃
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 14:07:23
    */
    public function getTaxonomy1($termId)
    {
        $continent_id=7;
        $SQL_SELECT_SYMBOLS="SELECT * FROM  country where continent_id=$termId";
        $SQL_SELECT_SYMBOLS="SELECT * FROM  country ";
        $sql='';

        if ($result=$mysqli->query($SQL_SELECT_SYMBOLS)) {
            if ($result->num_rows>0) {
                //echo '<table cellpadding="10" border="1">';
                //echo '<tr><th>id</th><th>country</th><th>animal</th><th>cname</th></tr>';
                while ($arr=$result->fetch_array()) {
                    $country_code=$arr['country_code'];
                    $flag=$codeArr[$country_code];
                    // $str.="INSERT INTO wp_terms (cid, name,slug,country_code,continent_id) VALUES ('$id', '$cname','$name','$country_code',$continent_id);"."\r\n";
                    // 'INSERT INTO wp_terms (cid, name,slug,country_code) VALUES ('$id', '$cname','$name','$country_code)
                    if ($codeArr[$country_code]=='') {
                        echo $arr['name'].'该国没有国旗,CODE'.$country_code."\r\n";
                    }
                    if ($codeArr[$country_code]!='') {
                       // $sql.='UPDATE `wp_terms` set flag ="'.$flag.'" WHERE country_code ="'. $country_code.'";'."\r\n";
                    }
             
                    //UNDATE `wp_terms` SET
                }
                echo $sql;
            // echo "</table>";
            } else {
                echo "记录未找到!";
            }
   
            $result->close(); //释放记录集所占用的内存
        } else {
            echo "error in query:$query.".$mysqli->error;
        }
    }
}

//$re =new Terms();
//$re->getCountryNameArr()->getTaxonomyArr()->getResult()->run();
